import { onBeforeUnmount, ref } from "vue";

const useCountdown = (time = 60) => {
  const countdown = ref(time);
  let start = ref(false);
  let timeId = null;
  const subCountdown = () => {
    start.value = true;
    clearInterval(timeId);
    timeId = setInterval(() => {
      countdown.value--;
      if (countdown.value <= 0) {
        clearInterval(timeId);
        start.value = false;
        countdown.value = time;
      }
    }, 1000);
  };
  onBeforeUnmount(() => {
    clearInterval(timeId);
  });
  return {
    countdown,
    start,
    subCountdown,
  };
};


export default useCountdown;
